//
//  MainViewController.swift
//  frptool
//
//  Created by 傅攀 on 2019/11/20.
//  Copyright © 2019 傅攀. All rights reserved.
//

import Cocoa
import INISerialization

class MainViewController: NSViewController {

    
    @IBOutlet weak var consoleTextField: NSTextField!
    
    @IBOutlet var consoleTextView: NSTextView!
    @IBOutlet weak var scrollTextView: NSScrollView!
    @IBOutlet weak var remoteAddrTextField: NSTextField!
    
    @IBOutlet weak var remoteServerPortTextField: NSTextField!
    
    @IBOutlet weak var localAddrTextField: NSTextField!
    @IBOutlet weak var localPortTextField: NSTextField!
    
    @IBOutlet weak var tokenTextField: NSTextField!
    @IBOutlet weak var remoteProxyPortTextFiled: NSTextField!
    
    
    var delegate: AppDelegate? = nil
    var frpcConfig: FRPCConfig? = nil
    
    var console: MyConsole? = nil
    
    var processManger: FRPCProcessManager? = nil
    
    var clientPath: String? = nil
    var configPath: String? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        
        delegate = NSApplication.shared.delegate as? AppDelegate
        
        
        console = MyConsole(textField: consoleTextView)
        
        frpcConfig = delegate!.getFrpcConfig()
        
      
       
        remoteAddrTextField.stringValue = frpcConfig?.common.server_addr ?? ""
        remoteServerPortTextField.stringValue = frpcConfig?.common.server_port ?? ""
        localAddrTextField.stringValue = frpcConfig?.ssh.local_ip ?? ""
        localPortTextField.stringValue = frpcConfig?.ssh.local_port ?? ""
    
        tokenTextField.stringValue = frpcConfig?.common.token ?? ""
        
        remoteProxyPortTextFiled.stringValue = frpcConfig?.ssh.remote_port ?? ""
        
        
        clientPath = delegate?.getClientPath()
        configPath = delegate?.getConfigPath()
        //构造进程管理
        processManger = delegate? .getProcessManager()
        processManger?.console = console
        
        
        
    
        


    }
    
  
    
    
    @IBAction func connectBtnClick(_ sender: Any) {
        
        if valueCheck() {
            //重新赋值并写回文件
            writeConfig()
            processManger?.connect()
        }
        
    }
    
    func  valueCheck()-> Bool{
        if remoteAddrTextField.stringValue.count == 0 {
            console?.error(text: "远程服务地址不能为空")
            return false
        }
        
        if remoteServerPortTextField.stringValue.count == 0{
            console?.error(text: "远程服务端口不能为空")
            return false
        }
        
        if localAddrTextField.stringValue.count == 0{
            console?.error(text: "本地转发地址不能为空")
            return false
        }
        
        if localPortTextField.stringValue.count == 0{
              console?.error(text: "本地转发端口不能为空")
              return false
        }
        
        if remoteProxyPortTextFiled.stringValue.count == 0{
              console?.error(text: "远程代理端口不能为空")
              return false
        }
        
        
        return true
    }
    
    
    func writeConfig(){
        frpcConfig?.common.server_addr = remoteAddrTextField.stringValue
        frpcConfig?.common.server_port = remoteServerPortTextField.stringValue
        frpcConfig?.common.token = tokenTextField.stringValue
        
        frpcConfig?.ssh.local_ip = localAddrTextField.stringValue
        
        frpcConfig?.ssh.local_port = localPortTextField.stringValue
        
        frpcConfig?.ssh.remote_port = remoteProxyPortTextFiled.stringValue
        delegate?.setFrpcConfig(frpconfig: frpcConfig!)
        
        
    }
    
    
    @IBAction func disconnectBtnClick(_ sender: Any) {
        processManger?.disconnect()
    }
    
    @IBAction func disconnectAllBtnClick(_ sender: Any) {
        processManger?.disconnectAll()
    }
    
}
